/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.DeptAO;
import com.lap.scp.admin.domain.DeptDO;
import com.lap.scp.admin.query.DeptQueryDO;

/**
 * <pre>
 * 部门管理控制对象
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Controller
@RequestMapping("dept")
public class DeptController {

	@Resource
	private DeptAO deptAO;

	@GetMapping(value = "add")
	public ModelAndView add() {
		ModelAndView andView = new ModelAndView("admin/deptEdit");
		andView.addObject("deptDO", new DeptDO());
		return andView;
	}

	@PostMapping(value = "add")
	public ModelAndView add(DeptDO deptDO, BindingResult error) {
		ModelAndView andView = new ModelAndView();

		Resp<DeptDO> resp = deptAO.insertDept(deptDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/dept/list");
		} else {
			andView.addObject("deptDO", deptDO);
			andView.setViewName("admin/deptEdit");
		}
		return andView;
	}

	@GetMapping(value = "delete/{deptId}")
	public ModelAndView deleteById(@PathVariable int deptId, @RequestParam Map<String, Object> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = deptAO.deleteById(deptId);
		if (resp.isSuccess()) {
			attributes.addAllAttributes(map);
			andView.setViewName("redirect:/dept/list");
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@GetMapping(value = "get/{deptId}")
	public ModelAndView get(@PathVariable int deptId) {
		ModelAndView andView = new ModelAndView();
		Resp<DeptDO> resp = deptAO.queryById(deptId);

		if (resp.isSuccess()) {
			andView.setViewName("admin/deptDetail");
			andView.addObject("deptDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryList(Page page, DeptQueryDO queryDO) {
		ModelAndView andView = new ModelAndView();

		PageResult<DeptDO> pr = deptAO.queryList(page, queryDO);
		if (pr.isSuccess()) {
			andView.setViewName("admin/deptList");
			andView.addObject("queryDO", queryDO);
			andView.addObject("pr", pr);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@GetMapping(value = "edit/{deptId}")
	public ModelAndView edit(@PathVariable int deptId) {
		ModelAndView andView = new ModelAndView();

		Resp<DeptDO> resp = deptAO.queryById(deptId);
		if (resp.isSuccess()) {
			andView.setViewName("admin/deptEdit");
			andView.addObject("deptDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@PostMapping(value = "edit")
	public ModelAndView edit(DeptDO deptDO, BindingResult error, @RequestParam Map<String, Object> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = deptAO.updateDept(deptDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/dept/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("admin/deptEdit");
			andView.addObject("deptDO", deptDO);
		}
		return andView;
	}

}
